const app = getApp();
var url = app.data.url;
import Dialog from '@vant/weapp/dialog/dialog';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    desk: [],
    tips: "提示:选择房间或餐台后请点击锁定!",
    value: "",
    uuid: "",
    merchantId: '',
    show: false,
    key: '',
    value: "",
    checked: false,
    btnType: 0,
    radio: "1",
    id:'',
    unitList:[],
    title:'',
    descType:''
  },
  
  onRadioChange: function(event) {
    console.log(event);
    let _that = this;
    this.setData({
      radio: event.detail
    });
  },
  onChange: function(event) {
    this.setData({
      checked: event.detail
    });
  },
  insertOrUpdate: function(option) {
    let _that = this;
    let key = _that.data.key;
    let value = _that.data.value;
    let merchantId = _that.data.merchantId;
    let uuid = _that.data.uuid;
    let descType = _that.data.descType;
    let method = option.currentTarget.dataset.method;
    let tempUrl;
    let tempData;
    if (method == 0) {
      tempUrl = url + '/HotelSystemApi/insertKeyValue';
      tempData = {
        key: key,
        value: value,
        merchantId: merchantId,
        uuid: uuid,
        descType: descType,
        keyType: 2
      }
    } else {
      tempUrl = url + '/HotelSystemApi/editKeyValue';
      let id = _that.data.id;
      tempData = {
        id: id,
        key: key,
        value: value,
        merchantId: merchantId,
        uuid: uuid,
        descType: descType,
        keyType: 2
      }
    }
    wx.request({
      url: tempUrl,
      data: tempData,
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function(res) {
        if (res.data.code == 200) {
          _that.setData({
            show: false
          })
          _that.queryData(uuid, merchantId, descType);
          wx.showToast({
            title: '操作成功!',
            icon: "success",
            duration: 2000
          })
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: "none",
            duration: 2000
          })
        }

      },
      fail: function(res) {
        console.info('submit fail');
      },
      complete: function(res) {
        console.info('submit complete');
      }
    })

  },
  /**表单 start */
  onKeyChange: function(option) {
    let _that = this;
    _that.setData({
      key: option.detail
    })
  },
  onValueChange: function(option) {
    let _that = this;
    _that.setData({
      value: option.detail
    })
  },
  /**
   * 删除
   */
  deleteUnit: function(option){
    let _that = this;
    let id = _that.data.id;
    let uuid = _that.data.uuid;
    let merchantId = _that.data.merchantId;
    let descType = _that.data.descType;
    wx.request({
      url: url + '/HotelSystemApi/deleteKeyValue',
      data: {
        id:id,
        uuid: uuid
      },
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        if (res.data.code == 200) {
          _that.setData({
            show: false
          })
          _that.queryData(uuid, merchantId, descType);
          wx.showToast({
            title: "删除成功!",
            icon: "success",
            duration: 2000
          })
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: "none",
            duration: 2000
          })
        }

      },
      fail: function (res) {
        console.info('submit fail');
      },
      complete: function (res) {
        console.info('submit complete');
      }
    }) 
  },
  /**表单 end */
  openUnitPop: function(option) {
    let _that = this;
    let type = option.currentTarget.dataset.type;
    if(type == 1){
      let key = option.currentTarget.dataset.key;
      let value = option.currentTarget.dataset.value;
      let id = option.currentTarget.dataset.id;
      _that.setData({
        id: id,
        key: key,
        value: value,
      })
    }
    _that.setData({
      btnType: type,
      show: true
    })
  },
  onClose() {
    this.setData({
      show: false
    });
  },
  onLoad: function(options) {
    let _that = this;
    let title = options.title;
    let descType = options.descType;
    let merchantId = options.merchantId;
    wx.getStorage({
      key: "uuid",
      success(res) {
        console.log(res);
        _that.setData({
          uuid: res.data
        })
        _that.queryData(res.data, merchantId, descType);
      }
    })
    _that.setData({
      title: title,
      descType: descType,
      merchantId: merchantId
    })

  },
  queryData: function(uuid, merchantId, descType){
    let _that = this;
    console.log(uuid);
    console.log(merchantId);
    console.log(descType);
    wx.request({
      url: url + '/HotelSystemApi/listPageKey',
      data: {
        uuid: uuid,
        merchantId: merchantId,
        descType: descType,
        keyType: 3
      },
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        if (res.data.code == 200) {
          console.log(res.data.data);
          _that.setData({
            unitList: res.data.data
          })
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: "none",
            duration: 2000
          })
        }
      },
      fail: function (res) {
        console.info('submit fail');
      },
      complete: function (res) {
        console.info('submit complete');
      }
    })
    
  },
  changeVal: function(e) {
    let _that = this;
    _that.setData({
      value: e.detail
    })
  },



})